#!/usr/bin/perl -w
# Register a repository into the global repo control system
# Usage:
#   perl regrepo.pl [PATH]
# PATH: path of repo to be registered, if not specified pwd will be used;

use File::Spec;

my $path=shift;
defined( $path) || ($path="./");
$path=File::Spec->rel2abs($path);
-e $path || die "Path $path does not exist!\n";
print "found repo at $path\n";
#my $cfg=`echo \$HOME`+"/.hyrepo.conf";
my $cfg="/etc/hyrepo.conf";

my $type= &get_protocol($path);
$type=~/unkown/ && die ( "Unkown repository type\n");

open FILE,"+<$cfg" || die ("Fail to read config file $cfg\n");
foreach my $l (<FILE>){
    if( $l=~/\S+\s+(\S+)/){
        ($1=~/$cfg/) or die "Repo $path already exist!\n";
    }
}

print FILE "$type $path\n";
close FILE;
print "Repo $type : $path added\n";
#   Get the protocal specific repo is using
#
sub get_protocol{
    my $p=shift;
    (-e "$p/.svn") && return "svn";
    (-e "$p/.git") && return "git";
    return "unkown";
}


